package com.eii.eiimq.mqtask;

import com.eii.eiimq.config.RabbitMQConfig;
import com.eii.eiimq.service.SmgTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalTime;

/**
 * @description:
 * @author: eii
 * @time: 2020/7/10 9:52 下午
 */
@Component
public class SendMsgTask {
    private static Logger logger = LoggerFactory.getLogger(SendMsgTask.class);

    @Autowired
    private SmgTaskService smgTaskService;
    /**
     * 发送消息，每3秒一条
     */
    @Scheduled(cron = "0/1 * * * * *")
    public void sendMsg() {

        LocalTime time = LocalTime.now();
        String msg = "producer msg:" + time.toString();
        logger.info("生产者 生成消息:{}", msg);

        //进行消息发送
        smgTaskService.publish(msg, RabbitMQConfig.EX_EIIMQ_SEND_MSG,RabbitMQConfig.EIIMQ_SEND_MSG_KEY);
    }


    //接收回复的消息
    @RabbitListener(queues = RabbitMQConfig.EIIMQ_RECV_MSG_QUEUE)
    public void receiveFinishChoosecourseTask(String backMsg){
//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        logger.info("生产者 接收 消费者处理结果:{}", backMsg);
    }
}
